<?php

namespace App;

use Illuminate\Support\Facades\Hash;
use Request;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    //指定表
    public $table = 'users';
    /*注册API*/
    public function signup(){

        $has_username_and_password = $this->has_username_and_password();

        if (!$has_username_and_password){
            return err('username and password require');
        }

        $username = $has_username_and_password[0];
        $password = $has_username_and_password[1];

        $user_exists = $this->where('username',$username)->exists();
        if($user_exists) {
            return err('username exists');
        }

        $user = $this;
        $user->password = bcrypt($password);
        $user->username = $username;
        if($user->save()){
            return suc('sign success',['id'=>$user->id]);
        }else{
            return err('db save err');
        }
    }
    /*登录API*/
    public function login(){
        $has_username_and_password = $this->has_username_and_password();

        if (!$has_username_and_password){
            return err('username and password require');
        }

        $username = $has_username_and_password[0];
        $password = $has_username_and_password[1];

        $user = $this->where('username',$username)->first();
        if (!$user){
            return err('username not exists');

        }

        $hashed_password = $user->password;
        if(!Hash::check($password,$hashed_password))
            return ['status'=>0,'msg'=>'password err'];
        session()->put('username',$user->username);
        session()->put('user_id',$user->id);
        return suc('success login',['id'=>$user->id]);
    }
    /*退出登录API*/
    public function logout(){
        //session()->set('persion.name','xiaoming');
        session()->forget('username');
        session()->forget('user_id');
        //return redirect('/');
        return suc();

    }
    /*检查用户是否登录*/
    public function is_logged_in(){
        return is_logged_in();
    }

    public function has_username_and_password(){
        $username = rq('username');
        $password = rq('password');
        if (!($username && $password)){
            return false;
        }else{
            return [$username,$password];
        }
    }


    /*修改密码API*/
    public function change_password(){
        if (!user_ins()->is_logged_in()){
            return err('login require');
        }
        if (!rq('old_password') || !rq('new_password')){
            return err('old and new _password require');
        }
        $user = $this->find(session("user_id"));
        if (Hash::check(rq('old_password'),$user->password)){
            return err('old_password error');
        }
        $user->password = bcrypt(rq('new_password'));
        if(!$user->save()){
            return suc();
        }else{
            return err('db error');
        }

    }
    /*重置密码API*/
    public function reset_password(){

        if (!$this->is_robot(10)){
            return err('err');
        }

        if (!rq('phone')){
            return err('phone require');
        }

        $res = $this->where('phone',rq('phone'))->first();
        if (!$res) err('phone not exists');
        $captcha = $this->generate_captcha();

        $res->phone_captcha = $captcha;
        if (!$res->save()){
            err('db err');
        }else{
            if (!$this->send_sms()){
                return err('send sms err');
            }
            $this->update_robot_time();

            return suc('success');
        }

    }



    /*验证验证码API*/
    public function validate_reset_password(){

        if (!$this->is_robot(2)){
            return err('is robot');
        }

        if (!rq('phone') || !rq('phone_captcha') ||!rq('new_password')){
            return err('phone ,new_password, phone captcha require');
        }
        $user = $this
            ->where(
                ['phone'=>rq('phone'),'phone_captcha'=>rq('phone_captcha')]
            )->first();
        if (!$user) return err('invalid phone');
        $this->update_robot_time();
        $user->password = bcrypt(rq('new_password'));
        if (!$user->save()) err('db err');
        return suc('suc');

    }

    /*是否是机器人*/
    public function is_robot($time){
        $current_time = time();
        $last_sms_time = session("last_sms_time");
        /*第一次的时候最后发送时间为空 所以直接返回true*/
        if (!$last_sms_time) return true;

        if ($current_time - $last_sms_time > $time){
            return true;
        }else{
            return false;
        }
    }
    /*获取用户信息API*/
    public function read(){
        if (!rq('id')){
            return err('require id');
        }
        $get = ['username','avatar_url','intro'];
        $user = $this->find(rq('id'),$get);
        $data =$user->toArray();
        $data['question'] = question_ins()->where(['user_id'=>rq('id')])->count();
        $data['answers'] = $this->answer()->count();
        return suc('success',$data);

    }
    /*更新机器人时间*/
    public function update_robot_time(){
        session()->set('last_sms_time',time());
    }

    /*发送短信API*/
    public function send_sms(){
        return true;
    }

    /*生成验证码*/
    public function generate_captcha(){
        return rand(1000,9999);
    }

    public function answer(){
        return $this
            ->belongsToMany('App\Answer')
            ->withPivot('vote')
            ->withTimestamps()
            ;
    }
    public function exists(){
        $username = rq('username');
        if (!$username) return err('用户名不可为空');
        $r = $this->where(['username'=>$username])->count();
        if ($r) return err('用户名已存在');
        return suc('用户名可用');
    }
}
